// import { drizzle } from 'drizzle-orm/libsql'
// import { sql } from 'drizzle-orm' // 导入 sql 辅助函数
// import { createClient } from '@libsql/client'
import * as schema from '~/utils/db/nuxt-schema'

// console.warn('config', config.DATABASE_URL || '')
// const client = createClient({ url: config.DATABASE_URL || '' })

// const turso = createClient({
//   url: config.TURSO_DATABASE_URL,
//   authToken: config.TURSO_AUTH_TOKEN
// })
// console.warn('config', turso)

import { drizzle } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
// const client = postgres(process.env.DATABASE_URL || '')
const client = postgres(useRuntimeConfig().DATABASE_URL)

export const nuxtdb = drizzle({
  client,
  schema,
  logger: true
})

// 测试查询（使用 sql 辅助函数）
async function testQuery(sqlstr: string) {
  try {
    // 对于较新的 Drizzle 版本，推荐使用 sql 模板字符串
    const result = await nuxtdb.execute(`${sqlstr}`)
    console.log('查询成功:', result)
  } catch (error) {
    console.error('查询失败:', error)
  }
}

testQuery('select 1')
testQuery('SELECT NOW()')
// testQuery('select * from emps')
// testQuery('SELECT * FROM sqlite_master WHERE type=\'table\' ORDER BY name')
